﻿//The MIT License(MIT)

//Copyright(c) 2015 Martin Chrzan

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
using System;
using System.ComponentModel;
using System.Windows;

namespace XmlAutoComplete
{
    public class AutocompleteItem : INotifyPropertyChanged, IComparable
    {
        private string item;
        public string Item
        {
            get { return item; }
            set { item = value; Notify("Item"); }
        }

        private string realName;
        public string RealName
        {
            get { return realName; }
            set { realName = value; Notify("RealName"); }
        }

        private string namespac;
        public string Namespace
        {
            get { return namespac; }
            set { namespac = value; Notify("Namespace"); }
        }

        private Visibility visibility = Visibility.Visible;
        public Visibility ItemVisibility
        {
            get { return visibility; }
            set { visibility = value; Notify("ItemVisibility"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public AutocompleteItem(string value)
        {
            Item = value;
            RealName = value;
            Namespace = "";
        }

        public AutocompleteItem(string value, string realName, string namespac)
        {
            Item = value;
            RealName = realName;
            Namespace = namespac;
        }


        public void Notify(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        public int CompareTo(object obj)
        {
            if (obj == null) return 0;

            if (obj is AutocompleteItem)
            {
                return this.Item.CompareTo((obj as AutocompleteItem).Item);
            }

            return 0;
        }


        public static implicit operator AutocompleteItem(DecisionTree tree)
        {
            var result = new AutocompleteItem(tree.Name);
            result.Namespace = tree.Namespace;

            if (tree.Abstract)
            {
                result.RealName = tree.BaseName + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"" + tree.Name + "\"";
            }
            else
            {
                result.RealName = tree.Name;
            }

            return result;
        }
    }
}
